/* $RCSFile: FolderInput.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/FolderInput.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Settings;
using AddonNice.UI.WebControls;
using System.Web.UI.HtmlControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.UI.DataTypes
{
	/// <summary>
	/// Implements a DropDownList with a list of directories 
	/// and a TextBox to add and create a new directory.
	/// </summary>
	public class FolderInput : CompositeControl
	{
		private string _dataValueField;
		private string _dataTextField;

        Control Combo                   =   null;
        Control NFText                  =   null;

        
        public  bool   _UsesRadControl  =   Config.UseRadControlsForSettings;

		public string DataValueField
		{
			get
			{
				return _dataValueField;
			}
			set
			{
				_dataValueField = value;
			}
		}

		public string DataTextField
		{
			get
			{
				return _dataTextField;
			}
			set
			{
				_dataTextField = value;
			}
		}

        object _DataSource  =   null;
        public object DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                _DataSource = value;
                SelectedValue   =   null;
            }
        }

        /// <summary>
        /// Selected value in Folders List
        /// </summary>
        public string SelectedValue
        {
            get
            {
                if (WebControlsTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput SelectedValue GET BEG ChildControlsCreated: {0}", ChildControlsCreated), WebControlsTraceSwitch.Sw.Info);
                if (!ChildControlsCreated)
                {
                    object ob   =   ViewState["SelectedValue"];
                    if ( ob != null )
                        return ob.ToString();
                    return string.Empty;
                }
                try
                {
                    if (_UsesRadControl)
                    {
                        RadComboBox dd = (RadComboBox)Combo;
                        return dd.SelectedValue;
                    }
                    else
                    {
                        DropDownList dd = (DropDownList)Combo;
                        return dd.SelectedValue;
                    }
                }
                catch (Exception ex)
                {
                    if (WebControlsTraceSwitch.Sw.TraceError)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FolderInput SelectedValue GET ex: {0}", ex), WebControlsTraceSwitch.Sw.Info);
                    throw;
                }
            }
            set
            {
                if (WebControlsTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput SelectedValue SET BEG ChildControlsCreated: {0}", ChildControlsCreated), WebControlsTraceSwitch.Sw.Info);
                if (!ChildControlsCreated)
                {
                    ViewState["SelectedValue"]  =   value;
                    return ;
                }
                try
                {
                    if (_UsesRadControl)
                    {
                        RadComboBox dd = (RadComboBox)Combo;
                        if (string.IsNullOrEmpty(value))
                            dd.ClearSelection();
                        else
                        {
                            RadComboBoxItem its = dd.Items.FindItemByValue(value);
                            if (its != null)
                                its.Selected = true;
                        }
                    }
                    else
                    {
                        DropDownList dd = (DropDownList)Combo;
                        if (string.IsNullOrEmpty(value))
                            dd.ClearSelection();
                        else
                        {
                            ListItem its = dd.Items.FindByValue(value);
                            if (its != null)
                                its.Selected = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (WebControlsTraceSwitch.Sw.TraceError)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FolderInput SelectedValue SET ex: {0}", ex), WebControlsTraceSwitch.Sw.Info);
                    throw;
                }
            }
        }

        /// <summary>
        /// New Folder in TextBox
        /// </summary>
        public string NewFolder
        {
            get
            {
                if (WebControlsTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput NewFolder GET BEG ChildControlsCreated: {0}", ChildControlsCreated), WebControlsTraceSwitch.Sw.Info);
                if (!ChildControlsCreated)
                {
                    return string.Empty;
                }
                try
                {
                    if (_UsesRadControl)
                    {
                        RadTextBox tb   = (RadTextBox)NFText;
                        if (WebControlsTraceSwitch.Sw.TraceVerbose)
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput NewFolder GET Val: {0}", tb.Text), WebControlsTraceSwitch.Sw.Info);
                        return tb.Text;
                    }
                    else
                    {
                        TextBox tb   = (TextBox)NFText;
                        if (WebControlsTraceSwitch.Sw.TraceVerbose)
                            Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput NewFolder GET Val: {0}", tb.Text), WebControlsTraceSwitch.Sw.Info);
                        if ( tb.Text.Equals(LocalizeHelper.Localize("NEW_FOLDER", "New Folder ?") ) )
                            return string.Empty;
                        return tb.Text;
                    }
                }
                catch (Exception ex)
                {
                    if (WebControlsTraceSwitch.Sw.TraceError)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FolderInput NewFolder GET ex: {0}", ex), WebControlsTraceSwitch.Sw.Info);
                    throw;
                }
            }
        }


        /// <summary>
        /// Ctor
        /// </summary>
        public FolderInput()
		{
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine("[V]FolderInput Ctor Called.", WebControlsTraceSwitch.Sw.Info);
		}

        //Construct controls
        protected override void CreateChildControls()
        {
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput CreateChildControls BEG UseRadControl: {0}", _UsesRadControl),WebControlsTraceSwitch.Sw.Info);
            ClearChildViewState();
            Controls.Clear();
			Panel panel                 =   new Panel();
            panel.ID                    =   "FdPnl";
            if (_UsesRadControl)
            {
                RadComboBox dd          = new RadComboBox();
                
                dd.DataSource           = DataSource;
                dd.DataValueField       = DataValueField;
                dd.DataTextField        = DataTextField;
                dd.Width                = new Unit((Width.Value / 2 - 1), Width.Type);
                dd.Style.Add("float","left");
                dd.ID                   = panel.ID + "_dd";
                if ( Width != Unit.Empty )
                        dd.Width                = new Unit((Width.Value / 2 - 1), Width.Type);
                else    dd.Width                = new Unit("80%");

                panel.Controls.Add(dd);
                panel.Controls.Add(new LiteralControl(StStr.HTML_SPACE));

                RadTextBox tb           = new RadTextBox();
                tb.EmptyMessage         = LocalizeHelper.Localize("NEW_FOLDER", "New Folder ?");
                tb.Columns              = 30;
                tb.Width                = dd.Width;
                dd.Style.Add("float","right");
                tb.ID                   = panel.ID + "_tb";
                Combo                   =   dd;
                NFText                  =   tb;
                panel.Controls.Add(tb);
            }
            else
            {
                DropDownList dd         = new DropDownList();
                dd.DataSource           = DataSource;
                dd.DataValueField       = DataValueField;
                dd.DataTextField        = DataTextField;
                dd.ID                   = panel.ID + "_dd";
                if ( Width != Unit.Empty )
                        dd.Width                = new Unit((Width.Value / 2 - 1), Width.Type);
                else    dd.Width                = new Unit("80%");

                panel.Controls.Add(dd);

                TextBox tb              = new TextBox();
                tb.CssClass             = "NormalTextBox";
                tb.Text                 = LocalizeHelper.Localize("NEW_FOLDER", "New Folder ?");
                tb.Columns              = 30;
                tb.Width                = dd.Width;
                tb.ID                   = panel.ID + "_tb";

                Combo                   =   dd;
                NFText                  =   tb;
                panel.Controls.Add(tb);
            }
            Controls.Add(panel);
            ChildControlsCreated        =   true;
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FolderInput CreateChildControls END.",WebControlsTraceSwitch.Sw.Info);
        }

        public override void DataBind()
        {
            if ( WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput DataBind BEG ChildControlsCreated: {0}",ChildControlsCreated),WebControlsTraceSwitch.Sw.Info);
            OnDataBinding(EventArgs.Empty);
            EnsureChildControls();
			try
			{
                if (_UsesRadControl)
                {
                    RadComboBox dd  =   (RadComboBox)Combo;
                    if ( WebControlsTraceSwitch.Sw.TraceVerbose)
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput DataBind RadComboBox found ClienID: {0}",dd.ClientID),WebControlsTraceSwitch.Sw.Info);
                    dd.ClearSelection();
                    dd.Items.Clear();
                    dd.DataBind();
                    if ( !string.IsNullOrEmpty(SelectedValue) )
                    {
                        RadComboBoxItem its = dd.Items.FindItemByValue(SelectedValue);
                        if (its != null)
                            its.Selected = true;
                    }
                    RadTextBox tb   =   (RadTextBox)NFText;
                    tb.Text  =   string.Empty;
                }
                else
                {
                    DropDownList dd = (DropDownList )Combo;
                    dd.ClearSelection();
                    dd.Items.Clear();
                    dd.DataBind();
                    if ( !string.IsNullOrEmpty(SelectedValue) )
                    {
                        ListItem its = dd.Items.FindByValue(SelectedValue);
                        if (its != null)
                            its.Selected = true;
                    }
                    TextBox tb      =   (TextBox)NFText;
                    tb.Text  =   string.Empty;
                }
			}
			catch (Exception ex)
			{
                if ( WebControlsTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FolderInput DataBind ex: {0}",ex),WebControlsTraceSwitch.Sw.Info);
                throw;
			}
            if ( WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FolderInput DataBind END ChildControlsCreated: {0}",ChildControlsCreated),WebControlsTraceSwitch.Sw.Info);
        }

        protected override void OnInit(EventArgs e)
        {
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FolderInput OnInit BEG.",WebControlsTraceSwitch.Sw.Info);
            base.OnInit(e);
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FolderInput OnInit END.",WebControlsTraceSwitch.Sw.Info);
        }


        protected override void Render(HtmlTextWriter writer)
        {
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FolderInput Render BEG.",WebControlsTraceSwitch.Sw.Info);
            if ( DesignMode )
                EnsureChildControls();
            try
            {
                base.Render(writer);
            }
            catch (Exception ex)
            {
                if ( WebControlsTraceSwitch.Sw.TraceError )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FolderInput Render ex: {0}",ex),WebControlsTraceSwitch.Sw.Info);
                throw;
            }
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FolderInput Render END.",WebControlsTraceSwitch.Sw.Info);
       }

	}
}